package com.cskaoyan.datasource;

import com.cskaoyan.utils.JDBCUtils;

import java.sql.Connection;
import java.util.LinkedList;
import java.util.List;

/**
 * 创建日期: 2022/10/10 14:59
 *
 * @author ciggar
 */
public class MyConnectionPoolV1 {

    // 定义一个容器，这个容器中存放的是多个连接对象
    // 从头部去取，从尾部去添加
    static LinkedList<Connection> pool;


    // 初始化数据库连接池
    static {

        pool = new LinkedList<>();

        for (int i = 0; i < 5; i++) {

            Connection connection = JDBCUtils.getNewConnection();
            pool.addLast(connection);

        }


    }


    // 获取连接
    public static Connection getConnection() {

        Connection connection = pool.removeFirst();

        return connection;

    }


    // 返回连接
    public static void returnConnection(Connection connection){

        pool.addLast(connection);

    }

}
